package com.z0ltrix.SMSitSupport;

import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.TimePicker;

public class SMSitSupportActivity extends Activity {
    /** Called when the activity is first created. */
	
	public static String MY_PREFS = "MY_PREFS";			//Shared space for saving preferences
	SharedPreferences mySharedPrefs; 					//Shared space for saving preferences
	TextView settingsDisplay;							//Home page text field
	static final int TIME_DIALOG_START_ID = 0;			//Constant for TimePicker
	static final int TIME_DIALOG_END_ID = 1;			//Constant for TimePicker
	private int startTimeHour;							//Support start hour. App will not do anything between support start and end times
	private int startTimeMin;							//Support start minute.
	private int endTimeHour;							//Support end hour. 24H format 
	private int endTimeMin;								//Support end minute.
	private String SMSKeyword;
	
	private static final int DEFAULT_START_HOUR = 8; 					//08:00 AM
	private static final int DEFAULT_START_MIN = 0;  					//08:00 AM
	private static final int DEFAULT_END_HOUR = 20; 					//08:00 PM
	private static final int DEFAULT_END_MIN = 0; 						//08:00 PM
	private static final String DEFAULT_SMS_SEARCH_KEYWORD = "Gentran"; //08:00 PM
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);     
        LoadPreferences();
       }
    
    
    private void LoadPreferences() {
    	
    	mySharedPrefs = getSharedPreferences(MY_PREFS, MODE_PRIVATE);
    	startTimeHour = mySharedPrefs.getInt("startTimeHour", DEFAULT_START_HOUR);
		startTimeMin  = mySharedPrefs.getInt("startTimeMin", DEFAULT_START_MIN);
		endTimeHour   = mySharedPrefs.getInt("endTimeHour", DEFAULT_END_HOUR);
		startTimeMin  = mySharedPrefs.getInt("endTimeMin", DEFAULT_END_MIN);
		SMSKeyword    = mySharedPrefs.getString("SMSKeyword", DEFAULT_SMS_SEARCH_KEYWORD);
		
		RefreshMenuDisplayValues();
		
		
		
	}


	private void RefreshMenuDisplayValues() {
		
		settingsDisplay = (TextView)findViewById(R.id.textSettings);
		
		new DateFormat();
		settingsDisplay.setText("Press Menu to set options.\n\nSupport Start Time-> "  + DateFormat.format("hh:mm a", new Date(0, 0, 0, startTimeHour, startTimeMin))
							+   "\nSupport End Time-> " + DateFormat.format("hh:mm a", new Date(0, 0, 0, endTimeHour, endTimeMin)) 
							+    "\nSMS Search Keyword-> " + SMSKeyword);
		
	}


	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	
		MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);   	
    	return super.onCreateOptionsMenu(menu);
    }
    
    
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	
    	switch(item.getItemId()){
    	
    	case R.id.itemSupportStartTime:
    		showDialog(TIME_DIALOG_START_ID);
    		return true;
    		
    	case R.id.itemSupportEndTime:
    		showDialog(TIME_DIALOG_END_ID);
    		return true;
    		
    	case R.id.itemSMSKeyWord:
    		showAlertForInput();
    		return true;
    	
    	case R.id.itemAbout:
    		showAboutBox();
    		return true;
    	
    	}
    	
    	return super.onOptionsItemSelected(item);
    }
    
    private void showAboutBox() {
    	
    	Context mContext = this;
    	Dialog dialog = new Dialog(mContext);

    	dialog.setContentView(R.layout.aboutdialogbox);
    	dialog.setTitle("Created by: Amit Parmar");

    	TextView text = (TextView) dialog.findViewById(R.id.textAboutid);
    	text.setText("This app will mute the phone for 5 seconds when a Gentran page is recieved after the support time has ended.\n" +
    				"\nWritten for Android devices with FroYo and above.\n" +
    				"If you have any suggestions, please e-mail to \nparmar.amit@gmail.com");
    	ImageView image = (ImageView) dialog.findViewById(R.id.image);
    	image.setImageResource(R.drawable.android);
    	
    	dialog.show();
		
	}


	private void showAlertForInput() {
    	AlertDialog.Builder alert = new AlertDialog.Builder(this);

    	alert.setTitle("SMS Keyword");
    	alert.setMessage("Case sensitive keyword");

    	final EditText input = new EditText(this);
    	alert.setView(input);
    	input.setText(SMSKeyword);

    	alert.setPositiveButton("Set", new DialogInterface.OnClickListener() {
    	public void onClick(DialogInterface dialog, int whichButton) {
    	  String value = input.getText().toString();
    	  SharedPreferences.Editor savePrefs = mySharedPrefs.edit();
			savePrefs.putString("SMSkeyword", value);
			savePrefs.commit();
			
			SMSKeyword = value;
			RefreshMenuDisplayValues();
    	  
    	  }
    	});

    	alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    	  public void onClick(DialogInterface dialog, int whichButton) {
    	    // Canceled. Do Nothing.
    	  }
    	});

    	alert.show();

		
	}

	private TimePickerDialog.OnTimeSetListener mTimeStartSetListener = new OnTimeSetListener() {
		
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			SharedPreferences.Editor savePrefs = mySharedPrefs.edit();
			savePrefs.putInt("startTimeHour", hourOfDay);
			savePrefs.putInt("startTimeMin", minute);
			savePrefs.commit();
			
			startTimeHour = hourOfDay;
			startTimeMin = minute;
			RefreshMenuDisplayValues();
			
		}
	};
	
	private TimePickerDialog.OnTimeSetListener mTimeEndSetListener = new OnTimeSetListener() {
		
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			SharedPreferences.Editor savePrefs = mySharedPrefs.edit();
			savePrefs.putInt("endTimeHour", hourOfDay);
			savePrefs.putInt("endTimeMin", minute);
			savePrefs.commit();
			
			endTimeHour = hourOfDay;
			endTimeMin = minute;
			RefreshMenuDisplayValues();
			
		}
	};
	
	
	protected Dialog onCreateDialog(int id) {
		
		switch(id){
		
		case TIME_DIALOG_START_ID:
			return new TimePickerDialog(this, mTimeStartSetListener, startTimeHour, startTimeMin, true);
			
		case TIME_DIALOG_END_ID:
			return new TimePickerDialog(this,mTimeEndSetListener,endTimeHour,endTimeMin,true );
				
		
		}
		
		return null;
	};
}